define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: true },
                        {
                            field: 'group_id', title: __('身份'), formatter: function (rwo) {
                                if(rwo>0){
                                    return '经销商';
                                }else{
                                    return '游客';
                                }
                            }
                        },
                        { field: 'username', title: __('Username'), operate: 'LIKE' },
                        { field: 'nickname', title: __('Nickname'), operate: 'LIKE' },
                        { field: 'qr_code', title: __('邀请码'), operate: 'LIKE' },
                        { field: 'superiors.username', title: __('推荐人'), operate: 'LIKE' },
                        { field: 'superiors.qr_code', title: __('推荐人邀请码'), operate: 'LIKE' },
                        { field: 'mobile', title: __('Mobile'), operate: 'LIKE' },
                        { field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        { field: 'region', title: __('区域'), operate: 'LIKE', sortable: true },
                        // { field: 'level', title: __('Level'), operate: 'BETWEEN', sortable: true },
                        // { field: 'level', title: __('Level'), operate: 'BETWEEN', sortable: true },
                        { field: 'money', title: __('Money'), operate: false, sortable: true },
                        { field: 'commission', title: __('佣金'), operate: false, sortable: true },
                        { field: 'frozen_commission', title: __('待结算佣金'), operate: false, sortable: true },
                        { field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: { normal: __('Normal'), hidden: __('Hidden') } },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'agencyarea',
                                    title: '查看',
                                    text: '代理授权区域',
                                    extend: 'data-area=["90%","90%"]',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'user/region/index',//接口地址 控制器名/方法名
                                },
                                {
                                    name: 'setgroup',
                                    title: '查看',
                                    text: '查看身份',
                                    extend: 'data-area=["90%","90%"]',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'user/info/index',//接口地址 控制器名/方法名
                                },
                                {
                                    name: 'setgroup',
                                    title: '查看',
                                    text: '信息',
                                    extend: 'data-area=["90%","90%"]',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'user/user/info',//接口地址 控制器名/方法名
                                },
                            
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});